package com.sw.bbs.board.controller;

import com.github.pagehelper.PageInfo;
import com.sw.bbs.board.pojo.Board;
import com.sw.bbs.board.vo.request.board.InsertBoardRequest;
import com.sw.bbs.board.vo.request.board.QueryBoardRequest;
import com.sw.bbs.board.vo.request.board.UpdateBoardRequest;
import com.sw.bbs.board.vo.response.board.BoardDetailResponse;
import com.sw.bbs.board.vo.response.board.BoardTreeResponse;
import com.sw.bbs.common.common.GlobalException;
import com.sw.bbs.common.common.ServiceException;
import com.sw.bbs.board.service.BoardService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Desc: 板块组API
 * <p>
 * 1.查询板块组列表
 * 2.查询板块详情
 * 3.更新板块的信息
 * 4.删除板块以及相关数据
 * 5.增加板块
 * ====
 * 6.板块统计
 *  - 相册/视频
 *  - 投票
 *  -
 * @Author: wusonghe
 * @Date: 2019/3/4 15:26
 */

@Api(value = "板块组模块", description = "板块组模块", tags = {"Board Module"})
@RequestMapping("/board")
@RestController
@Slf4j
public class BoardController {
    

    @Autowired
    private BoardService boardService;

    @GetMapping("/tree")
    @ApiOperation(value = "查询板块树形结构接口", notes = "查询树形板块结构", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 201, message = "树形板块", response = ResponseEntity.class)})
    public ResponseEntity<?> tree(HttpServletRequest httpServletRequest)
            throws GlobalException {
        try {
            List<BoardTreeResponse> list = boardService.queryboardTree();
            return new ResponseEntity<>(list, HttpStatus.OK);
        } catch (ServiceException e) {
            log.error("/board/tree", e);
            throw new GlobalException("/board/tree",e.getErrorCode().getMsg(),e.getErrorCode().getCode());
        }
    }

    @GetMapping("/list")
    @ApiOperation(value = "分页查询板块接口", notes = "分页查询板块帖子", response = PageInfo.class)
    @ApiResponses({@ApiResponse(code = 200, message = "分页结果结构", response = PageInfo.class),
            @ApiResponse(code = 201, message = "列表详情", response = Board.class)})
    public ResponseEntity<?> queryboardList(@ApiParam @ModelAttribute QueryBoardRequest queryRequest)
            throws GlobalException {
        try {
            PageInfo<Board> queryboardListResponse = boardService.queryboardList(queryRequest);
            return new ResponseEntity<>(queryboardListResponse, HttpStatus.OK);
        } catch (ServiceException e) {
            log.error("/board/list", e);
            throw new GlobalException("/board/list",e.getErrorCode().getMsg(),e.getErrorCode().getCode());

        }

    }

    @GetMapping("/detail")
    @ApiOperation(value = "查询板块详情接口", notes = "板块详情", response = BoardDetailResponse.class)
    @ApiResponses({@ApiResponse(code = 201, message = "列表详情", response = BoardDetailResponse.class)})
    public ResponseEntity<?> queryboardDetail(@ApiParam @ModelAttribute Integer boardId)
            throws GlobalException {

        try {

            BoardDetailResponse detailResponse = boardService.queryboardDetail(boardId);
            return new ResponseEntity<>(detailResponse, HttpStatus.OK);

        } catch (ServiceException e) {

            log.error("/board/detail", e);
            throw new GlobalException("/board/detail",e.getErrorCode().getMsg(),e.getErrorCode().getCode());

        }

    }

    @PostMapping("/insert")
    @ApiOperation(value = "新增板块接口", notes = "新增板块详情", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 200, message = "结果结构", response = ResponseEntity.class)})
    public ResponseEntity<?> insertboard(@ApiParam @ModelAttribute InsertBoardRequest insertboardRequest,
                                           HttpServletRequest httpServletRequest)
            throws GlobalException {
        try {
            Integer result = boardService.insertboardByRequest(insertboardRequest, httpServletRequest);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (ServiceException e) {
            log.error("/board/insert", e);
            throw new GlobalException("/board/insert",e.getErrorCode().getMsg(),e.getErrorCode().getCode());
        }

    }

    @PostMapping("/edit")
    @ApiOperation(value = "更新板块接口", notes = "更新板块", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 200, message = "结果结构", response = ResponseEntity.class)})
    public ResponseEntity<?> updateboard(@ApiParam @ModelAttribute UpdateBoardRequest updateboardRequest,
                                           HttpServletRequest httpServletRequest)
            throws GlobalException {

        try {
            Integer result = boardService.updateboardByRequest(updateboardRequest, httpServletRequest);
            return new ResponseEntity(result,HttpStatus.OK);
        } catch (ServiceException e) {
            log.error("/board/edit", e);
            throw new GlobalException("/board/edit",e.getErrorCode().getMsg(),e.getErrorCode().getCode());
        }

    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除板块接口", notes = "删除板块", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 200, message = "结果结构", response = ResponseEntity.class)})
    public ResponseEntity<?> deleteboard(@RequestParam Integer boardId)
            throws GlobalException {

        try {
            Integer result = boardService.deleteboardByRequest(boardId);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (ServiceException e) {
            log.error("/board/delete", e);
            throw new GlobalException("/board/delete",e.getErrorCode().getMsg(),e.getErrorCode().getCode());
        }

    }
}
